package com.city.subscription.controller;

import com.city.subscription.service.wxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Cheng
 * Date: 2020/10/21
 * Time: 23:39
 * Description: No Description
 */
@RestController
public class testController {
    @Autowired
    private wxService wxService;

    /**
     * 处理微信消息绑定后台
     * @param request
     * @return
     */
    @GetMapping("wx")
    public String getWx(HttpServletRequest request){
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        if (wxService.getToken(signature,timestamp,nonce)){
            return echostr;
        }else{
            return null;
        }
    }

    /**
     * 处理消息回复
     * @param request
     * @return
     */
    @PostMapping("wx")
    public String postWx(HttpServletRequest request){
        String respostText=null;
        try {
            Map<String,String> parseRequest =wxService.parseRequest(request.getInputStream());
            respostText=wxService.getRespost(parseRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return respostText;
    }
}
